package com.haiwen.cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 韩要雷
 *
 * 设置cookie的失效时间：
 * maxAge属性，默认值是-1，表示浏览器关闭失败
 *
 * 正整数
 *    表示存活在磁盘中的有效时间，单位为秒
 *
 * 负整数
 *    表示浏览器关闭失效
 *
 * 零
 *    即刻删除
 */
@WebServlet("/cookie03")
public class cookie03 extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //创建cookie
        Cookie cookie = new Cookie("aa","AA");

        //设置默认值-1--关闭浏览器就失效
        //cookie.setMaxAge(-1);

        //正整数
        cookie.setMaxAge(20);
        resp.addCookie(cookie);

        //设置七天失效
        Cookie cookie1 = new Cookie("name", "张三");
        cookie1.setMaxAge(7*24*60*60);
        resp.addCookie(cookie1);

        Cookie cookie2 = new Cookie("userPwd", null);
        cookie2.setMaxAge(0);
        resp.addCookie(cookie2);
    }
}
